package test.lesson

/**
 * author:vesper.z
 * date: 2024  16:17
 * class: Test09
 * description：数据类与密封类
 **/
class Test09 {

}

/**
 * 数据类
 * 可以创建一个只包含数据的类，关键字是data
 */
data class People(var name:String?,var age:Int?){
    init {//处理传入的值为null
        if (name ==null){
            name = "小明"
        }
        if (age ==null){
            age = 0
        }
    }
}
//复制：使用copy复制数据

//数据类以及解构说明

//标准数据类 Pair 和 Triple,

//密封类
sealed class Expor
data class Const(var number:Double): Expor()
data class Sum(var arg1:Double, var arg2: Double): Expor()

fun call(e: Expor):Double=  when (e){
    is Const -> e.number
    is Sum -> e.arg1+e.arg2
}

fun main(args: Array<String>) {

//密封类
    println(Math.ceil(call(Sum(1.2, 2.4))))

//    标准数据类Pair和Triple,需要指定数据类型和对应的具体的值
    var pair = Pair<String,String>("dd","ddd")
    var triple = Triple<String, Int, String>("1", 1, "2")


//    数据类以及解构说明
    var xiaoming = People("xiaoming", 19)
    val (name,age) = xiaoming
    println("name=$name,age=$age")

//    使用copy复制数据
    var zhangsan = People("张三", 19)
    var lisi = zhangsan.copy("李四")
    println("newPerson-> $lisi")

    var myData = People(null, null)
    println(myData.name)
    println(myData.age)
}